package org.ruiyichen.guizhou.agr;

import android.app.Application;
import android.os.Handler;

import org.greenrobot.eventbus.EventBus;
import org.ruiyichen.guizhou.agr.base.DataManger;
import org.ruiyichen.guizhou.agr.bean.MessageEvent;
import org.ruiyichen.guizhou.agr.config.Constant;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.net.okhttp.OkHttpUtils;
import org.ruiyichen.guizhou.agr.net.okhttp.log.LoggerInterceptor;
import org.ruiyichen.guizhou.agr.ui.mineview.BToast;
import org.ruiyichen.guizhou.agr.utils.L;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;


public class DigitalFieldApplication extends Application {

    private Handler handler;

    public static final String UPDATE_STATUS_ACTION = "com.umeng.message.example.action.UPDATE_STATUS";

    private boolean hasGotToken;


    //各个平台的配置，建议放在全局Application或者程序入口
    {
        //微信 wx12342956d1cab4f9,a5ae111de7d9ea137e88a5e02c07c94d
//        PlatformConfig.setWeixin("wx31cdd1b4ad09a5a8", "265d275462005bf21f7e1553abd0ad7f");
//        wx7e7bf8da5f8f2e38 0a9c71f333cd14dc674ea34872633b9f
        //豆瓣RENREN平台目前只能在服务器端配置
        //新浪微博
//        PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad");
        //易信
//        PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");
//        PlatformConfig.setQQZone("1106379811", "LjaugFEMMspD8dpI");
//        PlatformConfig.setTwitter("3aIN7fuF685MuZ7jtXkQxalyi", "MK6FEYG63eWcpDFgRYw4w9puJhzDl0tyuqWjZ3M7XJuuG7mMbO");
        //阿里
//        PlatformConfig.setAlipay("2015111700822536");
        //来往
//        PlatformConfig.setLaiwang("laiwangd497e70d4", "d497e70d4c3e4efeab1381476bac4c5e");
        //图钉网
//        PlatformConfig.setPinterest("1439206");

    }


    private OkHttpManager okHttpManager;


    @Override
    public void onCreate() {
        L.d("onCreate");
        super.onCreate();
//        UMShareAPI.get(this);

        DataManger.getInstance().init(this);

        BToast.getInstance().init(getApplicationContext());

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (Constant.App.IS_DEBUG) {
            builder.addInterceptor(new LoggerInterceptor("wu_tian"));
        }

        builder.addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
//                        .addHeader("Content-Type", "application/json;charset=UTF-8")
                        .addHeader("Content-Type", "application/json; charset=UTF-8")
//                        .addHeader("Accept-Encoding", "gzip, deflate")
//                        .addHeader("Connection", "keep-alive")
//                        .addHeader("Accept", "*/*")
                        .addHeader("Accept", "application/json")
//                        .addHeader("Cookie", "add cookies here")
                        .build();
                return chain.proceed(request);
            }
        });

        try

        {
//            OkHttpUtils.getInstance().setConnectTimeout(1000 * 10, TimeUnit.MILLISECONDS);
//            OkHttpUtils.getInstance().setReadTimeout(1000 * 10, TimeUnit.MILLISECONDS);
//            OkHttpUtils.getInstance().setWriteTimeout(1000 * 10, TimeUnit.MILLISECONDS);

            builder.connectTimeout(1000 * 10L, TimeUnit.MILLISECONDS)
                    .readTimeout(1000 * 10L, TimeUnit.MILLISECONDS)
                    .writeTimeout(1000 * 10L, TimeUnit.MILLISECONDS);
        } catch (Exception e
                )

        {
            e.printStackTrace();
        }

//        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
//        builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager);

        OkHttpUtils.initClient(builder.build());


        init();

//        initGallery();

        handler = new Handler();
        startRefresh();
    }


    private void startRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                L.e("wutian","EventBus 刷新");
                EventBus.getDefault().post(new MessageEvent(null));
                startRefresh();
            }
        }, 30 * 1000);
    }
    /**
     * 初始化相册
     */
//    private void initGallery() {
//        //建议在application中配置
//        //设置主题
//        ThemeConfig theme = ThemeConfig.BLUE;
////        ThemeConfig theme = new ThemeConfig.Builder()Builder
////                .build();
//        //配置功能
//        FunctionConfig functionConfig = new FunctionConfig.Builder()
//                .setEnableCamera(true)
//                .setEnableEdit(true)
//                .setEnableCrop(true)
//                .setEnableRotate(true)
//                .setCropSquare(true)
//                .setEnablePreview(true)
//                .setMutiSelectMaxSize(8)
//                .build();
//        CoreConfig coreConfig = new CoreConfig.Builder(this, new GlideImageLoader(), theme)
//                .setFunctionConfig(functionConfig)
//                .setPauseOnScrollListener(new UILPauseOnScrollListener(false, true))
//                .build();
//        GalleryFinal.init(coreConfig);
//    }


    /**
     * app初始化
     */
    private void init() {
//        JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志(极光推送Debug日志)
//        JPushInterface.init(this); // 初始化 JPush(极光推送)
//        // 初始化 JPush。如果已经初始化，但没有登录成功，则执行重新登录。
//        JPushInterface.init(this);
        //初始化imageLoader;
        setImageLoader();
        //初始化数据库
//        L.e(TAG, "wt-->ServiceFactory.getInstance().init(this);");
//        ServiceFactory.getInstance().init(this);

        //设置该CrashHandler为程序的默认处理器
//        if (!Constant.App.IS_DEBUG) {
//            UnCeHandler catchExcep = new UnCeHandler(this);
//            Thread.setDefaultUncaughtExceptionHandler(catchExcep);
//        }


        //自定义toast初始化


        //初始化数据类
        DataManger.getInstance().init(this);

    }


    /**
     * 配置ImageLoader框架
     */
    private void setImageLoader() {
//        GlideBuilder builder = Glide.get(this).

    }

}
